﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Collapse
{
    public partial class TableFieldValueDistributionEditor : Form,IComponentEditor 
    {
        private object _OwnerActivityNode;
        private TableFieldValueDistributionComponent _FieldValueDistributionComponent;

        public TableFieldValueDistributionEditor()
        {
            InitializeComponent();
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_ConnStr.Text))
            {
                string[] tbls = DbUtil.GetTables(this.txt_ConnStr.Text);
                this.cbo_Tables.Items.Clear();
                if (tbls != null && tbls.Length > 0)
                {
                    foreach (string tblName in tbls)
                    {
                        this.cbo_Tables.Items.Add(tblName);
                    }
                }
                this.cbo_Tables.SelectedIndex = -1;
                this.cbo_Tables.Text = string.Empty;

                if (!string.IsNullOrEmpty(this._FieldValueDistributionComponent.TableName))
                {
                    for (int i = 0; i < this.cbo_Tables.Items.Count; i++)
                    {
                        if (this.cbo_Tables.Items[i].ToString() == this._FieldValueDistributionComponent.TableName)
                        {
                            this.cbo_Tables.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(TableFieldValueDistributionComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._FieldValueDistributionComponent = (TableFieldValueDistributionComponent) wfComponent;

            this.txt_ConnStr.Text = this._FieldValueDistributionComponent.ConnStr;
            if (this.cbo_Tables.Items.Count > 0 && (!string.IsNullOrEmpty(this._FieldValueDistributionComponent.TableName)))
            {
                for (int i = 0; i < this.cbo_Tables.Items.Count; i++)
                {
                    if (this.cbo_Tables.Items[i].ToString() == this._FieldValueDistributionComponent.TableName)
                    {
                        this.cbo_Tables.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                this.cbo_Tables.Text = this._FieldValueDistributionComponent.TableName;
            }

            if(this.cbo_FieldName.Items.Count >0 && (!string.IsNullOrEmpty(this._FieldValueDistributionComponent.FieldName)))
            {
                for(int i=0;i<this.cbo_FieldName.Items.Count;i++)
                {
                    if(this.cbo_FieldName.Items[i].ToString()==this._FieldValueDistributionComponent.FieldName)
                    {
                        this.cbo_FieldName.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                this.cbo_FieldName.Text = this._FieldValueDistributionComponent.FieldName;
            }
        }

        public void SaveSettings()
        {
            this._FieldValueDistributionComponent.ConnStr = this.txt_ConnStr.Text;
            this._FieldValueDistributionComponent.TableName = this.cbo_Tables.Text;
            this._FieldValueDistributionComponent.FieldName = this.cbo_FieldName.Text;
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._FieldValueDistributionComponent);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbo_Tables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Tables.SelectedIndex>=0 && (!string.IsNullOrEmpty(this.txt_ConnStr.Text)))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this.txt_ConnStr.Text, string.Empty,
                                                            this.cbo_Tables.Text);
                this.cbo_FieldName.Items.Clear();
                this.cbo_FieldName.SelectedIndex = -1;
                this.cbo_FieldName.Text = string.Empty;

                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_FieldName.Items.Add(col.ColumnName);
                    }

                    if(!string.IsNullOrEmpty(this._FieldValueDistributionComponent.FieldName))
                    {
                        for (int i = 0; i < this.cbo_Tables.Items.Count; i++)
                        {
                            if (this.cbo_Tables.Items[i].ToString() == this._FieldValueDistributionComponent.TableName)
                            {
                                this.cbo_Tables.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
